package com.lesama.udp.chatroom;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.util.Scanner;

// UDP实现CS模式的聊天室

public class Client {
    public static void main(String[] args) throws IOException {
        DatagramSocket socket = new DatagramSocket(); // 随机指定端口号
        Scanner sc = new Scanner(System.in);
        while(true) {
            System.out.println("请输入要发送的消息：");
            String msg = sc.next();
            if(msg.equals("quit")) {
                System.out.println("退出聊天室");
                break;
            }
            byte[] bytes = msg.getBytes();
            DatagramPacket packet = new DatagramPacket(bytes, bytes.length, InetAddress.getByName("127.0.0.1"), 9999);
            socket.send(packet);
        }
        socket.close();
    }
}